package edu.byui.chat;
// Dave is a dork
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.ArrayList;

public class Client {

	private Socket clientSocket;
	private ObjectOutputStream oos;
	private ObjectInputStream ois;
	private String username;
	private ArrayList<Message> messages;
	private ArrayList<Message> readMessages;

	public static void main(String args[]) throws ClassNotFoundException {
		@SuppressWarnings("unused")
		Client c = new Client();
	}

	public Client() throws ClassNotFoundException{
		this.readMessages = new ArrayList<Message>();
		try{
			clientSocket = new Socket("localhost", 5555);
			oos = new ObjectOutputStream(clientSocket.getOutputStream());
			ois = new ObjectInputStream(clientSocket.getInputStream());
		}
		catch(Exception e){
			e.printStackTrace();
		}

		//Client gets input from user (a clients name)
		System.out.print("Enter your name: "); //prompt user for name
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); //open up standard input

		try {
			username = br.readLine();
		} catch (IOException e) {
			System.out.println("Error trying to read your name!");
			System.exit(-1);
		}

		try {
			Command c = new Command("login", username);
			oos.writeObject(c);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
		Thread input = new Thread(new ClientInThread(ois, this.messages, this.readMessages));
		Thread output = new Thread(new ClientOutThread(oos, ois, this.messages, this.readMessages, username));
		input.run();
		output.run();
	}
}